package com.tct.worktimeboard.controller;


import com.alibaba.fastjson.JSONObject;
import com.tct.worktimeboard.entity.Project;
import com.tct.worktimeboard.entity.Work;
import com.tct.worktimeboard.entity.WorkTask;
import com.tct.worktimeboard.service.IWorkService;
import com.tct.worktimeboard.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.logging.Logger;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyongkui
 * @since 2021-02-22
 */
@RestController
@RequestMapping("/work")
public class WorkController {

    @Autowired
    IWorkService workService;

    /**
     * 填报工时
     * @param work 工时数据
     * @return 返回填报状态
     */
    @PostMapping("/addWork")
    public JSONObject addWork(@RequestBody Work work){
        return workService.addWork(work);
    }

    /**
     * 删除工时
     * @param idList 删除ID列表
     * @return 返回删除状态
     */
    @PostMapping("/deleteWork")
    public JSONObject deleteWork(@RequestBody List<Integer> idList){
        return workService.deleteWork(idList);
    }

    /**
     * 修改工时填报信息
     * @param work 工时填报信息
     * @return 返回修改状态
     */
    @PostMapping("/updateWork")
    public JSONObject updateWork(@RequestBody Work work){
        return workService.updateWork(work);
    }

    /**
     * 查询工时填报信息
     * @param work 工时信息
     * @return 返回工时数据
     */
    @GetMapping("/search")
    public JSONObject search(Work work){
        return workService.getWork(work);
    }

    /**
     * 分页查询根据审批人和审批状态 未确认
     * @return
     */
    @GetMapping("/listPageByMakeUserIdAndMakeStatus")
    public JSONObject listPageByMakeUserIdAndMakeStatus(HttpServletRequest request,Work work){
        try{
            if(work != null && work.getMakeUserId() == null){
                String userId = request.getHeader("userId");
                work.setMakeUserId(Integer.parseInt(userId));
            }
        }catch (Exception e){
            return JsonUtil.toErrorData("request header 中 userId 错误");
        }
        return workService.listPageByMakeUserIdAndMakeStatus(work);
    }

    @GetMapping("/listPageByMakeUserIdAndMakeStatus/downloadExcel")
    public void listPageByMakeUserIdAndMakeStatusDownloadExcel(HttpServletResponse response,HttpServletRequest request,Work work) throws Exception {

        if(work != null && work.getMakeUserId() == null){
            String userId = request.getHeader("userId");
            work.setMakeUserId(Integer.parseInt(userId));
        }
        workService.listPageByMakeUserIdAndMakeStatusDownloadExcel(response,work);
    }

    /**
     * 分页查询根据申报人和审批状态 未通过
     * @return
     */
    @GetMapping("/listPageByUserIdAndMakeStatus")
    public JSONObject listPageByUserIdAndMakeStatus(HttpServletRequest request,Work work){
        try{
            if(work != null && work.getUserId() == null) {
                String userId = request.getHeader("userId");
                work.setUserId(Integer.parseInt(userId));
            }
        }catch (Exception e){
            return JsonUtil.toErrorData("request header 中 userId 错误");
        }
        return workService.listPageByUserIdAndMakeStatus(work);
    }

    @GetMapping("/listPageByUserIdAndMakeStatus/downloadExcel")
    public void listPageByUserIdAndMakeStatusDownloadExcel(HttpServletResponse response,HttpServletRequest request,Work work) throws Exception {
        if(work != null && work.getUserId() == null) {
            String userId = request.getHeader("userId");
            work.setUserId(Integer.parseInt(userId));
        }
        workService.listPageByUserIdAndMakeStatusDownloadExcel(response,work);
    }

    /**
     * 根据项目和项目下用户统计
     * @return
     */
    @GetMapping("/countGroupByProjectAndUserId")
    public JSONObject countGroupByProjectAndUserId(Project project){
        return JsonUtil.toJsonData(workService.countByProjectAndUserId(project));
    }

    /**
     * 根据项目和项目下用户统计 下载excel
     * @return
     */
    @GetMapping("/countGroupByProjectAndUserId/downloadExcel")
    public void countGroupByProjectAndUserIdDownloadExcel(HttpServletResponse response,Project project) throws Exception{
        workService.countGroupByProjectAndUserIdDownloadExcel(response,project);
    }

    /**
     * 根据部门分组查询
     * @return
     */
    @GetMapping("/countGroupByGroupType")
    public JSONObject countGroupByGroupType(Long groupTypeId){
        return workService.countGroupByGroupType(groupTypeId);
    }

    /**
     * 根据部门分组查询 下载Excel
     * @return
     */
    @GetMapping("/countGroupByGroupType/downloadExcel")
    public void countGroupByGroupTypeDownloadExcel(HttpServletResponse response,Long groupTypeId) throws Exception{
         workService.countGroupByGroupTypeDownloadExcel(response,groupTypeId);
    }

    /**
     * 根据负责人分组统计
     * @return
     */
    @GetMapping("/countGroupByUserId")
    public JSONObject countGroupByUserId(Work work){
        return workService.countGroupByUserId(work);
    }

    /**
     * 根据负责人分组统计 下载excel
     * @return
     */
    @GetMapping("/countGroupByUserId/downloadExcel")
    public void countGroupByUserIdDownloadExcel(HttpServletResponse response,Work work) throws Exception{
        workService.countGroupByUserIdDownloadExcel(response,work);
    }
    /**
     * 根据审批人人分组统计
     * @return
     */
    @GetMapping("/countGroupByMakeUserId")
    public JSONObject countGroupByMakeUserId(Work work){
        return workService.countGroupByMakeUserId(work);
    }

    /**
     * 根据审批人分组统计 下载excel
     * @return
     */
    @GetMapping("/countGroupByMakeUserId/downloadExcel")
    public void countGroupByMakeUserIdDownloadExcel(HttpServletResponse response,Work work)throws Exception {
        workService.countGroupByMakeUserIdDownloadExcel(response,work);
    }
    /**
     * 任务类型工时统计
     * @return
     */
    @GetMapping("/countGroupByWorkKindId")
    public JSONObject countGroupByWorkKindId(String workName){
        return workService.countGroupByWorkKindId(workName);
    }

    /**
     * 任务类型工时统计 下载excel
     * @return
     */
    @GetMapping("/countGroupByWorkKindId/downloadExcel")
    public void countGroupByWorkKindIdDownloadExcel(HttpServletResponse response,String workName) throws Exception{
        workService.countGroupByWorkKindIdDownloadExcel(response,workName);
    }

    /**
     * 根据任务统计工时
     * @param workTask
     * @return
     */
    @GetMapping("/countGroupByWorkTask")
    public JSONObject countGroupByWorkTask(WorkTask workTask){
        return workService.countGroupByWorkTask(workTask);
    }

    /**
     * 根据条件查询工时表
     * @return
     */
    @GetMapping("/listPageByCondition")
    public JSONObject listPageByCondition(Work work){
        return workService.listPageByCondition(work);
    }
}
